import 'module-alias/register';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { setupApp } from './setup';
import { getServerConfig } from '../ormconfig';
import Chalk from 'chalk';
import { Logger } from '@/common/log4js/log4j.util';
import { getLocalIP } from '@/common/utils/common';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';

async function bootstrap() {
    const config = getServerConfig();
    // 使用Fastify适配器创建应用
    const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter({
            logger: false, // 启用Fastify日志
        }),
        {
            cors: true, // 允许跨域
        },
    );

    await setupApp(app);

    const port = typeof config['APP_PORT'] === 'string'
        ? parseInt(config['APP_PORT'])
        : 3000;

    //一定要最后一句
    await app.listen({ port, host: '0.0.0.0' }); // Fastify推荐的监听方式
    const localIp = getLocalIP();

    Logger.log(
        `--------------------------------------------------`,
        '\n',
        Chalk.magenta(`服务启动成功 `),
        `http://localhost:${config.APP_PORT}${config.API_PREFIX}`,
        '\n',
        `             `,
        `http://${localIp}:${config.APP_PORT}${config.API_PREFIX}`,
        '\n',
        '\n',
        Chalk.green(`swagger文档地址`),
        `http://localhost:${config.APP_PORT}/doc.html`,
        '\n',
        `               `,
        `http://${localIp}:${config.APP_PORT}/doc.html`,
        '\n',
        `--------------------------------------------------`,
    );
}

bootstrap();


// import setupCluster from "./setup-cluster";
// setupCluster(bootstrap)
    